<template>
  <div class="company-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="登录日志" name="first">
        <vab-query-form>
          <vab-query-form-right-panel :span="26">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>
<!--              <el-form-item>
                <div style="width: 80px">搜索日志:</div>
              </el-form-item>-->
              <el-form-item>
                <el-input
                  v-model.trim="queryForm.keyword"
                  style="margin-right: 10px"
                  placeholder="请输入搜索关键词"
                  clearable
                />
              </el-form-item>
<!--              <el-form-item>
                <div style="width: 80px">创建时间：</div>
              </el-form-item>-->
              <el-form-item>
                <el-date-picker
                  v-model="queryForm.date"
                  type="daterange"
                  value-format="yyyy-M-d"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  @change="seleDate()"
                  style="width: 250px;"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  @click="handleSearch"
                >
                  查询
                </el-button>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-refresh-right"
                  @click="restart"
                >
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </vab-query-form-right-panel>
        </vab-query-form>

        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="序号"
            sortable
            type="index"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="日志内容"
            prop="log_content"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="操作人ID"
            prop="userid"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="操作人名称"
            prop="username"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="IP"
            prop="ip"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="耗时(毫秒)"
            prop="cost_time"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="日志类型"
          >
            <template #default="{ row }">
              {{ row.log_type == 1 ? '登录日志' : '操作日志' }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="创建时间"
            prop="create_time"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="操作日志" name="second">
        <vab-query-form>
          <vab-query-form-right-panel :span="26">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>
<!--              <el-form-item>
                <div style="width: 80px">搜索日志:</div>
              </el-form-item>-->
              <el-form-item>
                <el-input
                  v-model.trim="queryForm.keyword"
                  style="margin-right: 10px"
                  placeholder="请输入搜索日志关键词"
                  clearable
                />
              </el-form-item>
<!--              <el-form-item>
                <div style="width: 80px">创建时间：</div>
              </el-form-item>-->
              <el-form-item>
                <el-date-picker
                  v-model="queryForm.date"
                  type="daterange"
                  value-format="yyyy-M-d"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 250px;"
                />
              </el-form-item>
<!--              <el-form-item>
                <div style="width: 80px">操作类型：</div>
              </el-form-item>-->
              <el-form-item>
                <el-select
                  v-model="queryForm.options"
                  clearable
                  placeholder="请选择操作类型"
                  @change="selectStatus()"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  @click="handleSearch"
                >
                  查询
                </el-button>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-refresh-right"
                  @click="restart"
                >
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </vab-query-form-right-panel>
        </vab-query-form>

        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column type="expand">
            <template slot-scope="list">
              <el-form label-position="left">
                <el-form-item label="请求方法:">
                  <span>{{ list.row.method }}</span>
                </el-form-item>
                <el-form-item label="请求参数:">
                  <span>{{ list.row.request_param }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="序号"
            sortable
            type="index"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="日志内容"
            prop="log_content"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="操作人ID"
            prop="userid"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="操作人名称"
            prop="username"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="IP"
            prop="ip"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="耗时(毫秒)"
            prop="cost_time"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="日志类型"
          >
            <template #default="{ row }">
              {{ row.log_type == 1 ? '登录日志' : '操作日志' }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="操作类型"
            prop="operate_type"
          >
            <template #default="{ row }">
              {{
                row.operate_type == 1
                  ? '查询'
                  : row.operate_type == 2
                  ? '添加'
                  : row.operate_type == 3
                  ? '修改'
                  : row.operate_type == 4
                  ? '删除'
                  : row.operate_type == 5
                  ? '导入'
                  : '导出'
              }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            label="创建时间"
            prop="create_time"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import { getSysLogLists } from '@/api/common'

  export default {
    name: 'Admin',
    data() {
      return {
        options: [
          {
            value: 1,
            label: '查询',
          },
          {
            value: 2,
            label: '添加',
          },
          {
            value: 3,
            label: '修改',
          },
          {
            value: 4,
            label: '删除',
          },
          {
            value: 5,
            label: '导入',
          },
          {
            value: 6,
            label: '导出',
          },
        ],
        activeName: 'first',
        queryForm: {
          keyword: '',
          date: '',
          options: '',
          page: 1,
          list_row: 10,
        },
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        disabled: false,
        list: [],
        supplierCategories: [],
        menuList: '',
        date: '',
        types: 1,
      }
    },
    created() {
      this.getData()
    },
    methods: {
      handleDetail(scope) {
        this.$router.push({
          name: 'addIdentity',
          query: { id: scope.row.id, list: scope.$index },
        })
      },
      fetchData() {},
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          type: this.types,
          operate_type: this.queryForm.options,
          search: this.queryForm.keyword,
          date: this.date,
        }
        getSysLogLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      seleDate() {
        this.date =
          this.queryForm.date[0] + ' ' + '-' + ' ' + this.queryForm.date[1]
      },
      restart() {
        this.queryForm = []
        this.date = ''
        this.getData()
      },
      //搜索
      handleSearch() {
        this.getData()
      },
      handleClick(tab, event) {
        if (tab.index == 0) {
          this.types = 1
        } else {
          this.types = 2
        }
        this.getData()
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
    },
  }
</script>

<style lang="scss" scoped></style>
